package net.lab1024.sa.base.module.support.elasticsearch.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.base.common.domain.EventBase;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.module.support.elasticsearch.domain.form.EventBaseEsQueryForm;
import net.lab1024.sa.base.module.support.elasticsearch.domain.form.SoarDisposalForm;
import net.lab1024.sa.base.module.support.elasticsearch.service.EventBaseEsService;
import net.lab1024.sa.base.module.support.operatelog.annotation.OperateLog;
import net.lab1024.sa.base.module.support.target.EventInfoTarget;
import org.springframework.web.bind.annotation.*;
import java.lang.reflect.Field;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import net.lab1024.sa.base.module.support.elasticsearch.service.EventFieldConfigService;


/**
 * 安全事件 Controller
 *
 * @author chenyuqiang
 * @date 2025-01-27 10:00:00
 */
@Slf4j
@RestController
@Tag(name = "安全事件管理")
@RequestMapping("/api/elasticsearch")
@OperateLog
public class EventBaseController {

    @Autowired
    private EventBaseEsService eventBaseEsService;

    @Autowired
    private EventFieldConfigService eventFieldConfigService;

    @Operation(summary = "分页查询安全事件 @author chenyuqiang")
    @PostMapping("/event/queryPage")
    @SaCheckPermission("eventBase:query")
    public ResponseDTO<PageResult<EventBase>> queryPage(@RequestBody @Valid EventBaseEsQueryForm queryForm) {
        Integer opnum = queryForm.getOpnum();
        if (opnum == 1) {
            queryForm.setIndexName("event-base-*");
        }
        if (opnum == 2) {
            queryForm.setIndexName("event-alert-log-*");
        }
        if (opnum == 3) {
            queryForm.setIndexName("event-alert-white-*");
        }if (opnum == 4) {
            queryForm.setIndexName("event-alert-error-*");
        }

        PageResult<EventBase> eventBasePageResult = eventBaseEsService.queryPage(queryForm);
        List<EventBase> list = eventBasePageResult.getList();
        list.forEach(eventBase -> {
            eventBase.setRawLog("");
        });

        return ResponseDTO.ok(eventBasePageResult);
    }


    @Operation(summary = "SOAR处置 @author chenyuqiang")
    @PostMapping("/event/cz")
    public ResponseDTO<String> soarDisposal(@RequestBody @Valid SoarDisposalForm disposalForm) {
        return ResponseDTO.ok(eventBaseEsService.soarDisposal(disposalForm));
    }


    @GetMapping("/field/config")
    public ResponseDTO<Map<String, EventFieldConfigService.FieldConfig>> getFieldConfigs() {
        return ResponseDTO.ok(eventFieldConfigService.getFieldConfigs());
    }

    @Operation(summary = "获取属性值 @author chenyuqiang")
    @GetMapping("/event/targetinfo")
    public ResponseDTO<Map> targetinfo() {
        Map<String, String> stringStringMap = printFieldAnnotations(EventBase.class);
        return ResponseDTO.ok(stringStringMap);
    }
    @Operation(summary = "获取事件详情 @author chenyuqiang")
    @PostMapping("/event/detail")
    @SaCheckPermission("event:detail")
    public ResponseDTO<EventBase> getEventById(@RequestBody EventBaseEsQueryForm queryForm) {
        Integer opnum = queryForm.getOpnum();
        if (opnum == 1) {
            queryForm.setIndexName("event-base-*");
        }
        if (opnum == 2) {
            queryForm.setIndexName("event-alert-log-*");
        }
        if (opnum == 3) {
            queryForm.setIndexName("event-alert-white-*");
        }if (opnum == 4) {
            queryForm.setIndexName("event-alert-error-*");
        }

        return ResponseDTO.ok(eventBaseEsService.getEventById(queryForm.getEventId(),queryForm.getIndexName()));
    }

    @Operation(summary = "获取事件类型统计 @author chenyuqiang")
    @GetMapping("/event/stats/type")
    @SaCheckPermission("eventBase:stats")
    public ResponseDTO<Map<String, Object>> getEventTypeStats() {
        return ResponseDTO.ok(eventBaseEsService.getEventTypeStats());
    }

    @Operation(summary = "获取危险等级统计 @author chenyuqiang")
    @GetMapping("/event/stats/severity")
    @SaCheckPermission("eventBase:stats")
    public ResponseDTO<Map<String, Object>> getSeverityStats() {
        return ResponseDTO.ok(eventBaseEsService.getSeverityStats());
    }

    @Operation(summary = "获取地理位置统计 @author chenyuqiang")
    @GetMapping("/event/stats/geo")
    @SaCheckPermission("eventBase:stats")
    public ResponseDTO<Map<String, Object>> getGeoStats() {
        return ResponseDTO.ok(eventBaseEsService.getGeoStats());
    }

    @Operation(summary = "获取时间趋势统计 @author chenyuqiang")
    @PostMapping("/event/stats/trend")
    @SaCheckPermission("eventBase:stats")
    public ResponseDTO<Map<String, Object>> getTimeTrendStats(@RequestBody Map<String, Object> param) {
        return ResponseDTO.ok(eventBaseEsService.getTimeTrendStats(param));
    }

    @Operation(summary = "导出安全事件 @author chenyuqiang")
    @PostMapping("/event/export")
    @SaCheckPermission("eventBase:export")
    public ResponseDTO<String> exportEvents(@RequestBody @Valid EventBaseEsQueryForm queryForm) {
        return ResponseDTO.ok(eventBaseEsService.exportEvents(queryForm));
    }



    // 获取注解的值和属性名称的方法
    public static Map<String,String> printFieldAnnotations(Object obj) {
        Class<?> clazz = obj.getClass();
        Field[] fields = clazz.getDeclaredFields();
        Map<String,String> map = new HashMap<>();
        System.out.println("===== 注解信息 =====");
        for (Field field : fields) {
            if (field.isAnnotationPresent(EventInfoTarget.class)) {
                EventInfoTarget annotation = field.getAnnotation(EventInfoTarget.class);
                String fieldName = field.getName();
                String annotationName = annotation.name();
                map.put(fieldName,annotationName);
                System.out.println("字段名称: " + fieldName);
                System.out.println("注解名称: " + annotationName);
                System.out.println("------------------------");
            }
        }
        return map;
    }
} 